﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;

namespace com.dtn.deals
{
    public class tbl_UserManager : Itbl_UserManager
    {
        private static Itbl_UserManager itbl_UserManager = null;
        private tbl_UserManager()
        {
        }
        public static Itbl_UserManager CreateInstance()
        {
            if (itbl_UserManager == null)
            {
                itbl_UserManager = new tbl_UserManager();
            }
            return itbl_UserManager;
        }
        public bool Inserttbl_User(tbl_User _tbl_User) 
        {
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            bool ret = true;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.VarChar),
																new SqlParameter("@fullName", SqlDbType.NVarChar),
																new SqlParameter("@UserName", SqlDbType.VarChar),
																new SqlParameter("@Email", SqlDbType.NVarChar),
																new SqlParameter("@Pass", SqlDbType.NVarChar),
                                                                new SqlParameter("@RoleID", SqlDbType.Int),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
            parameters[0].Value = _tbl_User.ID;
            parameters[1].Value = _tbl_User.FullName;
            parameters[2].Value = _tbl_User.UserName;
            parameters[3].Value = _tbl_User.Email;
            parameters[4].Value = _tbl_User.Password;
            parameters[5].Value = _tbl_User.RoleID;
            parameters[6].Value = _tbl_User.CDate;
            try
            {
                dataProvider.ExecuteNonQuery("sp_InsertUpdatetbl_User", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }
        public bool Deletetbl_User(string ID) 
        {
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            bool ret = true;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.VarChar)
															};
            parameters[0].Value = ID;
           
            try
            {
                dataProvider.ExecuteNonQuery("sp_Deletetbl_User", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }
        public bool ChangePass_User(string ID, string pass) 
        {
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            bool ret = true;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.VarChar),
                                                                new SqlParameter("@pass", SqlDbType.VarChar)
															};
            parameters[0].Value = ID;
            parameters[1].Value = pass;

            try
            {
                dataProvider.ExecuteNonQuery("sp_ChangePasstbl_User", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }
        public DataTable Selecttbl_UserWithPagesDynamic(int StartIndex, int MaxRecords, string where) 
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parrams = { new SqlParameter("@Startindex",SqlDbType.Int ){Value = StartIndex },
                                         new SqlParameter("@Maxrecords",SqlDbType.Int ){Value = MaxRecords  },
                                         new SqlParameter("@WhereCondition",SqlDbType.NVarChar,500 ){Value = where  }
                                     };
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_UserWithPagesDynamic", CommandType.StoredProcedure, parrams);
            }
            catch
            {
                return null;
            }
            return ds.Tables["Table"];
        }
    }
}
